package com.vmladenov.cook.core;

import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.Date;

import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonElement;
import com.google.gson.JsonStreamParser;

public final class RestHelper
{

	public static <T> T GetJSON(String url, Class<T> classOfT)
	{
		if (!Helpers.isOnline(Helpers.getApplicationContext()))
			return null;
		
		HttpClient httpclient = new DefaultHttpClient();
		HttpGet httpGet = new HttpGet(url);
		try
		{
			HttpResponse response = httpclient.execute(httpGet);

			if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK)
			{
				return null;
			}

			Reader reader = new InputStreamReader(response.getEntity().getContent());

			GsonBuilder gsonb = new GsonBuilder();
			DateDeserializer ds = new DateDeserializer();
			gsonb.registerTypeAdapter(Date.class, ds);
			Gson gson = gsonb.create();
			return gson.fromJson(reader, classOfT);
		}
		catch (ClientProtocolException e)
		{
			e.printStackTrace();
		}
		catch (IOException e)
		{
			e.printStackTrace();
		}
		return null;
	}

	private static HttpResponse DoPost(String url, String parameter)
	{
		if (!Helpers.isOnline(Helpers.getApplicationContext()))
			return null;
		
		HttpClient httpclient = new DefaultHttpClient();
		HttpPost httpPost = new HttpPost(url);
		httpPost.addHeader("Content-Type", "application/json; charset=utf-8");
		try
		{
			StringEntity stringEntity = new StringEntity(parameter, "utf-8");

			httpPost.setEntity(stringEntity);
			return httpclient.execute(httpPost);
		}
		catch (ClientProtocolException e)
		{
			e.printStackTrace();
		}
		catch (IOException e)
		{
			e.printStackTrace();
		}
		return null;
	}

	public static <T> void JSONPost(String url, T parameter, Class<T> classOfT)
	{
		GsonBuilder gsonb = new GsonBuilder();
		Gson gson = gsonb.create();
		String json = gson.toJson(parameter, classOfT);
		DoPost(url, json);
	}

	public static <T> T JSONPost(String url, JsonElement element, Class<T> classOfT)
	{
		GsonBuilder gsonb = new GsonBuilder();
		Gson gson = gsonb.create();
		String json = gson.toJson(element);
		HttpResponse response = DoPost(url, json);
		if (response == null || response.getStatusLine().getStatusCode() != HttpStatus.SC_OK)
		{
			return null;
		}
		try
		{
			Reader reader = new InputStreamReader(response.getEntity().getContent());
			return gson.fromJson(reader, classOfT);
		}
		catch (IllegalStateException e)
		{
			e.printStackTrace();
		}
		catch (IOException e)
		{
			e.printStackTrace();
		}
		return null;
	}

	public static JsonElement JSONPost(String url, JsonElement element)
	{
		GsonBuilder gsonb = new GsonBuilder();
		Gson gson = gsonb.create();
		String json = gson.toJson(element);
		HttpResponse response = DoPost(url, json);
		if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK)
		{
			return null;
		}
		try
		{
			Reader reader = new InputStreamReader(response.getEntity().getContent());
			JsonStreamParser parser = new JsonStreamParser(reader);
			if (parser.hasNext()) return parser.next();
			return null;
		}
		catch (IllegalStateException e)
		{
			e.printStackTrace();
		}
		catch (IOException e)
		{
			e.printStackTrace();
		}
		return null;
	}

}
